package jp.heartdokyun.activity;

import java.util.HashMap;

import jp.heartdokyun.R;
import jp.heartdokyun.listener.TopBackListener;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class RewardCardActivity extends Activity {

	/** Called when the activity is first created. */
	private SoundPool mSoundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	private String stageType;
	private MediaPlayer mMediaPlayer;
	private Bitmap mBitmap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reward_card);
		TextView tvTitle = (TextView) findViewById(R.id.tv_reward_card_title);
		initSounds();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		try {
			Thread.sleep(500);
		} catch (Exception e) {
		}
		;
		// tvTitle.setText(getIntent().getStringExtra("CHALLENGE_TYPE"));
		stageType = getIntent().getStringExtra("CHALLENGE_TYPE_VALUE");
		String Level = getIntent().getStringExtra("LEVEL");

		mMediaPlayer = MediaPlayer.create(getApplicationContext(),
				R.raw.other_bgm);
		float volume = Utils.getBGMVolume(getApplicationContext());
		mMediaPlayer.setVolume(volume, volume);
		mMediaPlayer.setLooping(true);

		ImageButton ibBack = (ImageButton) findViewById(R.id.ib_return_menu);
		ibBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itMenu = new Intent(RewardCardActivity.this,
						MenuActivity.class);

				startActivity(itMenu);
			}
		});

		ImageButton ibo = (ImageButton) findViewById(R.id.ic_o);
		ibo.setOnClickListener(new TopBackListener(this));

		if (Level.equals("")) {
			Level = "1";
		}

		int imageID = getResources().getIdentifier(stageType + "_lv" + Level,
				"drawable", "jp.heartdokyun");

		mBitmap = BitmapFactory.decodeResource(getResources(), imageID);

		ImageView ivViewCard = (ImageView) findViewById(R.id.iv_reward_card);
		ivViewCard.setImageBitmap(mBitmap);

		String Title = getResources().getString(R.string.game_tilte) + " "
				+ getIntent().getStringExtra("CHALLENGE_TYPE") + " Lv." + Level;
		tvTitle.setText(Title);

		ImageButton btnReturn = (ImageButton) findViewById(R.id.btn_back);
		btnReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent itGameSuccess = new Intent(RewardCardActivity.this,
						GameSuccessActivity.class);
				itGameSuccess.putExtra("CHALLENGE_TYPE", getIntent()
						.getStringExtra("CHALLENGE_TYPE"));
				itGameSuccess.putExtra("CHALLENGE_TYPE_VALUE", getIntent()
						.getStringExtra("CHALLENGE_TYPE_VALUE"));
				startActivity(itGameSuccess);
			}
		});
		ImageButton ibCardCollection = (ImageButton) findViewById(R.id.ib_main_card_collection);
		ibCardCollection.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itMenu = new Intent(RewardCardActivity.this,
						CardCollectionActivity.class);
				startActivity(itMenu);
			}
		});

		ImageButton btnStageSelect = (ImageButton) findViewById(R.id.btn_next_stage);
		ImageButton btnReturnMenu = (ImageButton) findViewById(R.id.btn_return_menu);
		if (Level.equals("5")) {
			btnStageSelect.setVisibility(View.INVISIBLE);
			btnReturnMenu.setVisibility(View.VISIBLE);
		}

		btnStageSelect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent itNextGame = new Intent(RewardCardActivity.this,
						GameActivity.class);
				itNextGame.putExtra("CHALLENGE_TYPE", getIntent()
						.getStringExtra("CHALLENGE_TYPE"));

				itNextGame.putExtra("CHALLENGE_TYPE_VALUE", getIntent()
						.getStringExtra("CHALLENGE_TYPE_VALUE"));
				int nextlevel = Integer.parseInt(getIntent().getStringExtra(
						"LEVEL")) + 1;
				Utils.addLevel(RewardCardActivity.this, getIntent()
						.getStringExtra("CHALLENGE_TYPE_VALUE"));
				itNextGame.putExtra("LEVEL", Integer.toString(nextlevel));
				startActivity(itNextGame);
			}
		});
		btnReturnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itMenu = new Intent(RewardCardActivity.this,
						MenuActivity.class);

				startActivity(itMenu);

			}
		});

	}
	
	public void onStop(){
		Log.d(getClass().getSimpleName(), "onStop");
		if (mBitmap != null)
			mBitmap.recycle();
		super.onStop();
	}

	@Override
	public void onDestroy() {
		Log.d(getClass().getSimpleName(), "onDestroy");
		mSoundPool.release();
		mMediaPlayer.release();
		super.onDestroy();
	}

	@Override
	public void onStart() {
		Log.d(getClass().getSimpleName(), "onStart");
		if (stageType.equals("ca")) {
			playSound(1);
		} else if (stageType.equals("nurse")) {
			playSound(2);
		} else if (stageType.equals("ol")) {
			playSound(3);
		} else if (stageType.equals("minikimono")) {
			playSound(4);
		} else if (stageType.equals("bloomer")) {
			playSound(5);
		} else if (stageType.equals("teacher")) {
			playSound(6);
		} else if (stageType.equals("sailor")) {
			playSound(7);
		} else if (stageType.equals("maid")) {
			playSound(8);
		}
		super.onStart();
	}

	@Override
	public void onResume() {
		Log.d(getClass().getSimpleName(), "onResume");
		if (MainActivity.isEnableBGM) {
			mMediaPlayer.start();
		} else {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.pause();
				mMediaPlayer.seekTo(0);
			}
		}
		super.onResume();
	}

	@Override
	public void onPause() {
		Log.d(getClass().getSimpleName(), "onPause");
		if (mMediaPlayer.isPlaying()) {
			mMediaPlayer.pause();
			mMediaPlayer.seekTo(0);
		}
		mSoundPool.release();
		super.onPause();
	}

	private void initSounds() {
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.put(1, mSoundPool.load(getApplicationContext(),
				R.raw.otsukaresamadeshita_ca, 1));
		soundPoolMap.put(2, mSoundPool.load(getApplicationContext(),
				R.raw.suggoiyokattayo_nurse, 1));
		soundPoolMap.put(3, mSoundPool.load(getApplicationContext(),
				R.raw.matashitekuremasuka_ol, 1));
		soundPoolMap.put(4, mSoundPool.load(getApplicationContext(),
				R.raw.matakiteokunnamashi_jp, 1));
		soundPoolMap.put(5, mSoundPool.load(getApplicationContext(),
				R.raw.yarujan_bloomers, 1));
		soundPoolMap.put(6, mSoundPool.load(getApplicationContext(),
				R.raw.yarebadekirujanai_teacher, 1));
		soundPoolMap.put(7, mSoundPool.load(getApplicationContext(),
				R.raw.miechaimashita_sailor, 1));
		soundPoolMap.put(8, mSoundPool.load(getApplicationContext(),
				R.raw.hazukashiidesu_maid, 1));
	}

	// yarujan_bloomers
	//
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	public void playSound(int sound) {
		float volume = Utils.getVoiceVolume(getApplicationContext());
		mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
	}
}